import * as franService from '../services/franService';
import { decryptRes, dealPagination } from '../utils/requestUtil';
import { message } from 'antd';

export default {
  namespace: 'fran',

  state: {
    franList: [],
    franListPagination: {},

    franDetail: null,

    franMember: [],
    franMemberPagination: {},

    orderRecord: [],
    orderRecordPagination: {},

    exportFranMember: [],
    exportOrderRecord: [],
  },

  effects: {
    *fetchFranList({ payload }, { call, put }) {
      console.log('前端参数11111121212', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranList, payload)));
      console.log('接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'franListReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchDetail({ payload, callback }, { call, put }) {
      const response = JSON.parse(decryptRes(yield call(franService.fetchDetail, payload)));
      console.log('加盟商详情接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'detailReducer',
          payload: { data },
        });
        if (callback) callback(data);
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *addFran({ payload, callback }, { call, put }) {
      console.log('addFran前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.addFran, payload)));
      console.log('addFran接口返回数据：', response);

      if (response.code === 0) {
        message.success('添加成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *editFran({ payload, callback }, { call, put }) {
      console.log('editFran前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.editFran, payload)));
      console.log('editFran接口返回数据：', response);

      if (response.code === 0) {
        message.success('修改成功！')
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *changeFranStatus({ payload, callback }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.changeFranStatus, payload)));
      console.log('接口返回数据：', response);

      if (response.code === 0) {
        message.success('操作成功！');
        if (callback) callback();
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchFranMember({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranMember, payload)));
      console.log('加盟商关联会员接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'franMemberReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchExportFranMember({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchFranMember, payload)));
      console.log('加盟商关联会员接口返回数据：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'exportFranMemberReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchOrderRecord({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchOrderRecord, payload)));
      console.log('接口返回数据fetchOrderRecord----：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'orderRecordReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },

    *fetchExportOrderRecord({ payload }, { call, put }) {
      console.log('前端参数', payload)
      const response = JSON.parse(decryptRes(yield call(franService.fetchOrderRecord, payload)));
      console.log('接口返回数据fetchOrderRecord----：', response);
      if (response.code === 0) {
        let data = response.data;
        yield put({
          type: 'exportOrderRecordReducer',
          payload: { data },
        });
      } else {
        message.error('调用服务器出错!' + response.msg);
      }
    },
  },

  reducers: {
    franListReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        franList: action.payload.data.dataList,
        franListPagination: dealPagination(action.payload.data.pageInfo),
      };
    },
    detailReducer(state, action) {
      console.log(action.payload.data)
      return {
        ...state,
        franDetail: action.payload.data,
      };
    },
    franMemberReducer(state, action) {
      console.log("franMemberfranMemberfranMemberfranMemberfranMemberfranMemberfranMember",action.payload.data)
      return {
        ...state,
        franMember: action.payload.data.dataList ? action.payload.data.dataList : [],
        franMemberPagination: dealPagination(action.payload.data.pageInfo),
      };
    },

    orderRecordReducer(state, action) {
      return {
        ...state,
        orderRecord: action.payload.data.dataList ? action.payload.data.dataList : [],
        orderRecordPagination: dealPagination(action.payload.data.pageInfo),
      };
    },

    exportFranMemberReducer(state, action) {
      console.log("franMemberfranMemberfranMemberfranMemberfranMemberfranMemberfranMember",action.payload.data)
      return {
        ...state,
        exportFranMember: action.payload.data.dataList ? action.payload.data.dataList : [],
      };
    },

    exportOrderRecordReducer(state, action) {
      return {
        ...state,
        exportOrderRecord: action.payload.data.dataList ? action.payload.data.dataList : [],
      };
    },
  },
};
